import {
  Button,
  Form,
  FormItem,
  Image,
  Input,
  InputNumber,
  message,
  Modal,
  Radio,
  RadioGroup,
  Space,
  TypographyText
} from 'ant-design-vue'
import { cloneDeep } from 'lodash-es'
import { defineComponent, ref } from 'vue'

import { materialApi } from '@/apis'
import { BlockItem } from '@/apis/materialTypes'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import UploadFile from '@/components/UploadFile'
import { TABLE_FORMAT } from '@/constants'
import { useDialog } from '@/hooks'

export default defineComponent({
  name: 'IconSetting',
  setup() {
    const { showConfirm } = useDialog()

    const showIconDialog = ref(false)
    const iconDialogLoading = ref(false)

    const optionItemCur = ref({} as BlockItem)

    const tableRef = ref({} as ProTableRef)
    const tableConfig = ref({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',

        columns: [
          {
            title: '标题',
            field: 'title'
          },
          {
            title: '图标',
            width: 100,
            slots: {
              default: 'image'
            }
          },
          {
            title: '排序',
            field: 'rank'
          },

          {
            title: '创建时间',
            field: 'createTime',
            minWidth: 200,
            formatter: [TABLE_FORMAT.Date, 'yyyy-MM-dd HH:mm:ss']
          },
          {
            title: '状态',
            fixed: 'right',
            width: 100,
            slots: {
              default: 'status'
            }
          },
          {
            title: '操作',
            fixed: 'right',
            slots: {
              default: 'operation'
            },
            width: 150
          }
        ],

        proxyConfig: {
          props: {
            result: 'data'
          },
          ajax: {
            query: async () => {
              const data = await materialApi.getBlockList()
              return data
            }
          }
        }
      }
    } as ProTableProps)

    //更新配置
    function updateIcon(item) {
      optionItemCur.value = cloneDeep(item)
      showIconDialog.value = true
    }
    async function updateIconConfirm() {
      if (!optionItemCur.value.title) {
        message.error('请输入标题')
        return
      }
      if (!optionItemCur.value.iconUrl) {
        message.error('请选择图标')
        return
      }

      try {
        iconDialogLoading.value = true
        await materialApi.editBlockItem(optionItemCur.value)
        showIconDialog.value = false
        tableRef.value.refresh()
      } finally {
        iconDialogLoading.value = false
      }
    }

    //启用禁用
    function changeStatus(item) {
      showConfirm({
        content: item.status == 0 ? '确认启用该图标？' : '确认禁用该图标',
        onOk: async () => {
          try {
            await materialApi.forbidBlockItem(item.id, item.status == 0 ? 1 : 0)
            tableRef.value.refresh()
          } catch (error) {}
        }
      })
    }

    return {
      tableRef,
      tableConfig,
      showIconDialog,
      iconDialogLoading,
      optionItemCur,
      updateIcon,
      updateIconConfirm,
      changeStatus
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            image: ({ row }) =>
              row.iconUrl ? <Image src={row.iconUrl}></Image> : null,
            status: ({ row }) => {
              return (
                <Space>
                  {row.status == 0 ? (
                    <TypographyText type="danger">禁用</TypographyText>
                  ) : (
                    <TypographyText type="success">启用</TypographyText>
                  )}
                </Space>
              )
            },
            operation: ({ row }) => (
              <Space>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.updateIcon(row)}
                >
                  编辑
                </Button>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.changeStatus(row)}
                >
                  {row.status == 0 ? '启用' : '禁用'}
                </Button>
              </Space>
            )
          }}
          {...this.tableConfig}
        />
        <Modal
          v-model:visible={this.showIconDialog}
          width={600}
          destroyOnClose
          title="编辑"
          onOk={this.updateIconConfirm}
        >
          <Form labelCol={{ span: 4 }}>
            <FormItem label={'标题'} required>
              <Input
                placeholder={'请输入'}
                v-model:value={this.optionItemCur.title}
                style={{ width: '300px' }}
                maxlength={8}
                v-slots={{
                  suffix: () => (
                    <span>
                      {this.optionItemCur.title?.length || 0}
                      /8
                    </span>
                  )
                }}
              />
            </FormItem>
            <FormItem label={'图标'} required>
              <UploadFile v-model={[this.optionItemCur.iconUrl, 'urls']}>
                上传图片
              </UploadFile>
            </FormItem>
            <FormItem label="跳转链接">
              <RadioGroup v-model:value={this.optionItemCur.jumpLink}>
                <Radio value={1}>内部跳转</Radio>
                <Radio value={2}>外部跳转</Radio>
              </RadioGroup>
            </FormItem>

            <FormItem label={' '} colon={false}>
              <Form labelCol={{ span: 4 }}>
                <FormItem label="path地址" style={{ marginTop: '-10px' }}>
                  <Input
                    placeholder={'请输入'}
                    v-model:value={this.optionItemCur.pathAdress}
                    style={{ width: '300px' }}
                  />
                </FormItem>
                {this.optionItemCur.jumpLink == 2 ? (
                  <FormItem label="appId" style={{ marginTop: '-10px' }}>
                    <Input
                      placeholder={'请输入'}
                      v-model:value={this.optionItemCur.appId}
                      style={{ width: '300px' }}
                    />
                  </FormItem>
                ) : null}
                {this.optionItemCur.jumpLink == 2 ? (
                  <FormItem label="打开方式" style={{ marginTop: '-10px' }}>
                    <RadioGroup v-model:value={this.optionItemCur.openMode}>
                      <Radio value={1}>半屏显示</Radio>
                      <Radio value={2}>全屏显示</Radio>
                    </RadioGroup>
                  </FormItem>
                ) : null}
              </Form>
            </FormItem>

            <FormItem label={'排序'}>
              <InputNumber
                placeholder={'请输入'}
                v-model:value={this.optionItemCur.rank}
                style={{ width: '300px' }}
                min={1}
                maxlength={4}
              />
              <p style={{ marginTop: '10px', color: '#888' }}>
                请输入大于等于1的整数，值越小排前面
              </p>
            </FormItem>
            <FormItem label="状态" required>
              <RadioGroup v-model:value={this.optionItemCur.status}>
                <Radio value={1}>启用</Radio>
                <Radio value={0}>禁用</Radio>
              </RadioGroup>
            </FormItem>
          </Form>
        </Modal>
      </div>
    )
  }
})
